package com.version1_4.service.implement;

import com.util.format.ReevesMap;
import com.util.property.Properties;
import com.version1_3.dao.ActivityDao;
import com.version1_3.dao.UserDao1_3;
import com.version1_4.dao.ActivityDao1_4;
import com.version1_4.service.ActivityService1_4;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by reeveslee on 16/5/22.
 */
@Service
public class ActivityServiceImpl1_4 implements ActivityService1_4 {
    private static final Logger LOGGER = Logger.getLogger(ActivityServiceImpl1_4.class);

    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private UserDao1_3 userDao13;

    @Autowired
    private ActivityDao1_4 activityDao14;


    @Override
    public String activityMainPage(int userId) {

        List<Map<String, Object>> activityBannerList = activityDao14.getActivityInfoByType(0);
        List<Map<String, Object>> activityList = activityDao.getActivityInfoByType(1);
        String userJoinedActivityInfo = userDao13.getUserJoinedActivityIds(userId);


        ReevesMap reevesMap = new ReevesMap();
        reevesMap.put("banner", activityBannerList); // 活动主页banner

        // 检测用户是否参与了活动
        for (Map activity : activityList) {

            if (userId != -1) {
                // 如果是跬步公益健身
                if ((long) activity.get("activityID") == Properties.KbActivity_jianshenID) {
                    if (userJoinedActivityInfo.contains(String.valueOf(Properties.KbActivity_fuwocheng)) || userJoinedActivityInfo.contains(String.valueOf(Properties.KbActivity_pingban)) || userJoinedActivityInfo.contains(String.valueOf(Properties.KbActivity_yangwo))) {
                        // 用户已经参加了跬步健身相关活动,需要发送用户坚持了多少天
                        activity.put("insistDays", activityDao.getInsistDays(userId, (int) Properties.KbActivity_jianshenID));
                        activity.put("hasJoined", true);
                    } else {
                        activity.put("hasJoined", false);
                    }
                } else if (userJoinedActivityInfo.contains(String.valueOf(activity.get("activityID")))) {
                    // 用户已经参加了跬步公益跑和骑行活动,需要发送用户坚持了多少天
                    activity.put("insistDays", activityDao.getInsistDays(userId, (int) ((long) activity.get("activityID"))));
                    activity.put("hasJoined", true);
                } else {
                    activity.put("hasJoined", false);
                }
            } else {
                // 未登陆用户
                activity.put("hasJoined", false);
            }
        }

        reevesMap.put("activities", activityList); // 活动列表

        return reevesMap.toJsonString();
    }

    @Override
    public String activityInfo(int activityId) {
        return activityDao14.activityInfo(activityId);
    }
}
